using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// author:Zeng shunbo
    /// </summary>
    public partial class AssetItemList : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140803";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(AssetItemList_ChangeLanguage);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.grdAssetItemList.PageSize = this.GridViewPageDefaultSize();
                SetLanguage();
                Display();
            }

            /* Comment by jack 20151023 for U274 */
            ScriptManager.RegisterStartupScript(Page, typeof(string), "", "InitCombobox();", true);
        }

        #endregion

        #region Some private function.

        private void Display()
        {
            //check access right
            FunctionItemSetting();
            BindAssetItemList();

            BindAssetType();
            //this.ddlAssetType.SelectedValue = assetItemInfo.AssetTypeID.ToString(); u274
        }

        /// <summary>
        /// Get selected items value.
        /// </summary>
        /// <param name="stationIDs">selected stationIDs.</param>
        private void SetStationSelectedItem(ref string stationIDs)
        {
            int selectedStation = ddlStation.SelectedItem == null ? -2 : Convert.ToInt32(ddlStation.SelectedValue);

            if (selectedStation == 0 || selectedStation == -1)
            {
                foreach (ListItem item in ddlStation.Items)
                {
                    stationIDs = stationIDs + "," + item.Value;
                }
            }
            else
            {
                stationIDs = selectedStation.ToString();
            }
        }

        //check access right of setting function item.
        private void FunctionItemSetting()
        {
            if (DAL.Security.User.CanFunctionItemAccess("M140803_F2", this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else if (DAL.Security.User.CanFunctionItemAccess("M140803_F3", this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                //BindStation
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);
                this.ddlStation.Enabled = false;
            }

            if (DAL.Security.User.CanFunctionItemAccess("M140803_F1", this.ODMUser.UserProfileID, this.MenuID)) //readonly
            {
                this.btnNew.Enabled = false;
            }

            //update by gordon 20151020 U247
            //if (ddlStation.Items.FindByValue(this.ODMUser.StationID.ToString()) != null)//set current station.
            //{
            //    this.ddlStation.SelectedValue = this.ODMUser.StationID.ToString();
            //}
            if (ddlStation.Items.FindByValue(this.ODMUser.LocationID.ToString()) != null)//set current station.
            {
                this.ddlStation.SelectedValue = this.ODMUser.LocationID.ToString();
            }
        }
        

        //BindGrid
        private void BindAssetItemList()
        {            
            this.grdAssetItemList.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);
            int selectedStation = ddlStation.SelectedItem == null ? -1 : Convert.ToInt32(ddlStation.SelectedValue);
            string stationIDs = "";
            SetStationSelectedItem(ref stationIDs,this.ddlStation);
            /*Comment by jack 20151117 for u274*/
            //DataTable assetItemTable = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetItem.GetAssetItemList(0, this.LangCode, 0, this.txtAssetType.Text.Trim(),this.txtAssetItem.Text.Trim(), stationIDs, this.chkVoid.Checked);
            //DataTable assetItemTable = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetItem.GetAssetItemList(0, this.LangCode, 0, this.ddlAssetType.SelectedIndex==-1? "" : this.ddlAssetType.SelectedItem.Text.Trim(), this.txtAssetItem.Text.Trim(), stationIDs, this.chkVoid.Checked);
            DataTable assetItemTable = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetItem.GetAssetItemList(0, this.LangCode, 0, txtSelectedAssetType.Value.Trim(), this.txtAssetItem.Text.Trim(), stationIDs, this.chkVoid.Checked, true); // //modify by tim 20160409 for save stockqty
            
            DataView dvAssetItem = assetItemTable.DefaultView;
            dvAssetItem.Sort = this.grdAssetItemList.DataViewSortExpression;
            this.grdAssetItemList.DataSource = dvAssetItem;
            this.grdAssetItemList.DataBind();

            assetItemTable.Dispose();
            assetItemTable = null;
        }

        #endregion 

        #region SetLanguage

        void AssetItemList_ChangeLanguage(object sender, EventArgs e)
        {
            SetLanguage();
            Display();
        }

        private void SetLanguage()
        {
            this.lblStation.Text = GetLabel("SOM_Station", true);
            this.lblAssetType.Text = GetLabel("SOM_AssetType", true);
            this.lblIncludeVoid.Text = GetLabel("COM_Show", true);
            //this.lblMenuCaption.Text = GetLabel("SOM_AssetItem");
            this.btnSearch.Text = GetLabel("COM_Search");
            this.btnNew.Text = GetLabel("SOM_AddNewAssetLoanItem");
            this.chkVoid.Text = GetLabel("SOM_Voided");
            this.lblAssetItem.Text = GetLabel("SOM_AssetItem",true);

            /* Comment by jack 20151103 for U274 */
            this.btnClearAssetType.Text = GetLabel("SOM_ClearAssetType");
        }

        #endregion

        #region Some Event.

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindAssetItemList();
            ScriptManager.RegisterStartupScript(Page, typeof(string), "", "InitCombobox();", true);
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AssetItemEntry.aspx?action=NEW");
        }        

        protected void grdAssetItemList_NeedDataBind()
        {
            BindAssetItemList();
        }

        protected void grdAssetItemList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "EDIT")
            {
                Response.Redirect("AssetItemEntry.aspx?action=Edit&assetItemID=" + e.CommandArgument.ToString());
            }
        }

        /*Comment by jack 20151030 for U274*/
        protected void btnClearAssetType_Click(object sender, EventArgs e)
        {
            /*Comment by jack 20151117 for u274*/
            this.txtSelectedAssetType.Value = "";
            BindAssetType();
            ScriptManager.RegisterStartupScript(Page, typeof(string), "", "InitCombobox();", true);
        }

        private void BindAssetType()
        {
            int selectedStation = this.ddlStation.SelectedItem != null ? Convert.ToInt32(ddlStation.SelectedValue.ToString()) : 0;
            string stationIDs = selectedStation.ToString();

            BindListControl(this.ddlAssetType, "AssetTypeID", "AssetTypeDesc", ODMS.DAL.SOM.CodeSetup.CDAssetType.GetAssetTypeList(stationIDs, "", true, this.LangCode), true, "");
        }
        #endregion
    }
}
